package com.bosssoft.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibDepartmentDTO;
import com.bosssoft.entity.po.FibDepartmentPO;
import com.bosssoft.entity.vo.FibDepartmentVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibDepartmentService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 部门表(FibDepartment)表控制层
 *
 * @author makejava
 * @since 2021-08-20 10:59:16
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping("/fibDepartment")
public class FibDepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private FibDepartmentService fibDepartmentService;

    /**
     * 查询指定id
     *
     * @return
     */
    @PostMapping("/select")
    public Result select(@Validated @RequestBody Long id) {
        try {
            FibDepartmentDTO records = fibDepartmentService.selectOneById(id);
            return Result.ok().message("查询成功").data("list",records);
        } catch (Exception e) {
            return Result.error().code(1).message("查询失败");
        } finally {
        }
    }

    @PostMapping("/selectAll")
    public Result selectAll(@RequestParam(defaultValue = "1") Integer pageNo,
                            @RequestParam(defaultValue = "10") Integer pageSize,FibDepartmentVO fibDepartmentVO) {
        try {
            FibDepartmentPO fibDepartmentPO = new FibDepartmentPO();
            BeanUtils.copyProperties(fibDepartmentVO,fibDepartmentPO);
            Page<FibDepartmentPO> page = new Page<>(pageNo,pageSize);
            PageResult<FibDepartmentPO> pageList = fibDepartmentService.page(page, fibDepartmentPO);
            return Result.ok().data("page",pageList);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }
//    @PostMapping("/selectAll")
//    public Result selectAll(@RequestBody Long page,Long limit) {
//        try {
//            List<FibDepartmentDTO> records = fibDepartmentService.selectAll(page,limit);
//            return Result.ok().message("查询成功").data("list",records);
//        } catch (Exception e) {
//            return Result.error().code(1).message("查询失败");
//        } finally {
//        }
//    }
    /**
     * 根据id删除
     *
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@Validated @RequestBody FibDepartmentVO fibDepartmentVO) {
        FibDepartmentDTO fibDepartmentDTO = new FibDepartmentDTO();
        BeanUtils.copyProperties(fibDepartmentVO, fibDepartmentDTO);
        boolean delete = fibDepartmentService.delete(fibDepartmentDTO);
        if (delete) {
            return Result.ok().code(1).message("删除账户成功");
        }else {
            return Result.error().code(2).message("删除账户失败");
        }
    }
    /**插入一条数据
     * @return*/
    @PostMapping("/insert")
    public Result insert(@Validated @RequestBody FibDepartmentVO fibDepartmentVO) {
        FibDepartmentDTO fibDepartmentDTO = new FibDepartmentDTO();
        BeanUtils.copyProperties(fibDepartmentVO, fibDepartmentDTO);
        boolean insert = fibDepartmentService.insert(fibDepartmentDTO);
        if (insert) {
            return Result.ok().code(1).message("添加账户成功");
        }else {
            return Result.error().code(2).message("添加账户失败");
        }
    }
    /**更新数据
     * @return*/
    @PostMapping("/update")
    public Result update(@Validated @RequestBody FibDepartmentVO fibDepartmentVO) {
        FibDepartmentDTO fibDepartmentDTO = new FibDepartmentDTO();
        BeanUtils.copyProperties(fibDepartmentVO, fibDepartmentDTO);
        boolean update = fibDepartmentService.update(fibDepartmentDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户成功");
        }else {
            return Result.error().code(2).message("更新账户失败");
        }
    }
    /**
     * 更新激活状态
     * @return
     *
     */
    @PostMapping("/updateDeleted")
    public Result updateDeleted(@Validated @RequestBody FibDepartmentVO fibDepartmentVO){
        FibDepartmentDTO fibDepartmentDTO = new FibDepartmentDTO();
        BeanUtils.copyProperties(fibDepartmentVO, fibDepartmentDTO);
        boolean update = fibDepartmentService.updateDeleted(fibDepartmentDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户激活状态成功");
        }else {
            return Result.error().code(2).message("更新账户激活状态失败");
        }
    }

    //预算编报需要的接口
    @PostMapping("/selectChildrenIds")
    @ApiOperation("根据部门id查询其所有子部门id")
    public List<Long> selectChildrenIds(Long deptId){
        List<Long> childrenIds = fibDepartmentService.selectChildrenIds(deptId);
        return childrenIds;
    }

    @PostMapping("/selectChildrenPage")
    @ApiOperation("根据ids获取部门列表（分页）")
    public Result selectChildrenPage(@RequestParam(defaultValue = "1")Integer pageNo,
                                     @RequestParam(defaultValue = "10")Integer pageSize,
                                     Long deptId){
        Page<FibDepartmentVO> page = fibDepartmentService.selectChildrenPage(pageNo,pageSize,deptId);
        PageResult<FibDepartmentVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page",pageResult);
    }
}
